//
//  NewadviceViewController.m
//  maocuhuiOASystem
//
//  Created by michael on 16/5/4.
//  Copyright © 2016年 czd. All rights reserved.
//
#import "OANameListController.h"
#import "NewadviceViewController.h"
#import "selectupboxView.h"
#import "MHDatePicker.h"
#import "selectdownbox.h"
#import "OACityNameViewController.h"
#import "OASubNameListModel.h"
@interface NewadviceViewController ()<OANameListControllerDelegate>
@property (strong, nonatomic) MHDatePicker *selectTimePicker;
@property (strong, nonatomic) MHDatePicker *selectDatePicker;
@property (strong, nonatomic)selectupboxView * checkBoxFootView;
@property (strong, nonatomic)selectdownbox * checkBoxFootView1;
@property(nonatomic,copy)NSString * body;
@property(nonatomic,copy)NSString * string1;
@property(nonatomic,copy)NSString * string2;
@property(nonatomic,copy)NSString * datastring;


@end

@implementation NewadviceViewController

- (void)viewDidLoad {



    [super viewDidLoad];
     [self setupTextFieldRightView];
    // Do any additional setup after loading the view.
    //(_sendTextField.size.height)*.9
 _checkBoxFootView  = [selectupboxView checkBoxFootView];
 NSLog(@"%f,%f",_sendTextField.size.width, (_sendTextField.size.height));
    _checkBoxFootView.size = CGSizeMake(200, 30);
//      checkBoxFootView.size = _subjectTextField.size;
// _checkBoxFootView.backgroundColor = [UIColor redColor];
     [self.upselectview addSubview:_checkBoxFootView];
         _checkBoxFootView1 = [selectdownbox checkBoxFootView];
//             checkBoxFootView1.size = _sendTextField.size;
 _checkBoxFootView1.size = CGSizeMake(200, 30);
//  _checkBoxFootView.backgroundColor = [UIColor yellowColor];

     [self.downselectview addSubview:_checkBoxFootView1];
     self.title = _string;
      UIBarButtonItem * rightItem = [[UIBarButtonItem alloc]initWithTitle:@"发送" style:UIBarButtonItemStylePlain target:self action:@selector(operation)];
    self.navigationItem.rightBarButtonItem = rightItem;
}
-(void)operation{
    [self keybordDown];
    
    if ([self.sendTextField.text length] == 0) {
        [self.view makeToast:@"对不起，你的标题不可以为空"];
        return;
    }
    if ([self.subjectTextField.text length] == 0) {
          [self.view makeToast:@"对不起，你的通知范围不可以为空"];
        return;
    }
    
    if ([self.datalabel.text length] ==0) {
          [self.view makeToast:@"对不起，你的截止日期不可以为空"];
        return;
    }
    if ([self.contentText.text length] ==0) {
          [self.view makeToast:@"对不起，你的内容不可以为空"];
        return;
    }
    
    [self uploadEmail];
    
}
- (void)uploadEmail{

    NSMutableDictionary * dict = [NSMutableDictionary dictionary];
    //标题
  if (self.sendTextField.text.length !=0) {
        [dict setObject:self.sendTextField.text forKey:@"标题"];
    }
 //通知范围
    if (self.subjectTextField.text.length !=0) {
        [dict setObject:self.subjectTextField.text forKey:@"通知范围"];
    }
 //内容
    if (self.contentText.text.length !=0) {
        [dict setObject:self.contentText.text forKey:@"内容"];
    }
     //截止日期
    if (self.datalabel.text.length !=0) {
        [dict setObject:self.datalabel.text forKey:@"截止日期"];
    }
// 公告分类
        [dict setObject:self.checkBoxFootView.orderString forKey:@"公告分类"];
        if ([self.checkBoxFootView.orderString isEqualToString:@"0"]) {
    _string1 = @"公告";
} else if ([self.checkBoxFootView.orderString isEqualToString:@"1"]) {
    _string1 = @"会议通知";
}
else{
    _string1 = @"通知";

} if ([self.checkBoxFootView1.orderString isEqualToString:@"0"]) {
    _string2 = @"保留";
} else  {
    _string2 = @"删除";
}
_datastring = [self.datalabel.text stringByReplacingOccurrencesOfString:@"年" withString:@"-"];
_datastring = [_datastring stringByReplacingOccurrencesOfString:@"月" withString:@"-"];
_datastring = [_datastring stringByReplacingOccurrencesOfString:@"日" withString:@""];
        [dict setObject:self.checkBoxFootView1.orderString forKey:@"到期后"];

     self.body = [NSString stringWithFormat:@"&lt;?xml version=\'1.0\' encoding=\'utf-8\'?&gt;\n"
                     "&lt;交换 xmlns=\'exch.jhsx.gov.cn/data\' xmlns:xsi=\'http://www.w3.org/2001/XMLSchema-instance\'&gt;\n"
                     "&lt;数据&gt;\n"
                     "&lt;数据元素&gt;\n"
                     "&lt;标题&gt;\n"
                     "%@"
                     "&lt;/标题&gt;\n"
                     "&lt;通知范围&gt;\n"
                     "%@"
                     "&lt;/通知范围&gt;\n"
                     "&lt;公告分类&gt;\n"
                     "%@"
                     "&lt;/公告分类&gt;\n"
                     "&lt;截止日期&gt;\n"
                     "%@"
                     "&lt;/截止日期&gt;\n"
                     "&lt;到期后&gt;\n"
                     "%@"
                     "&lt;/到期后&gt;\n"
                     "&lt;内容&gt;\n"
                     "%@"
                     "&lt;/内容&gt;\n"
                    "&lt;/数据元素&gt;\n"
                     "&lt;/数据&gt;\n"
                     "&lt;/交换&gt;",
                     self.sendTextField.text,
                     self.subjectTextField.text,
                     self.string1,
                     self.datastring,
                     self.string2,
                     self.contentText.text];

    
    NSData * data  = [NSJSONSerialization dataWithJSONObject:dict options:0 error:NULL];
    NSString * infoString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    
    NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];

NSArray * array =@[@{@"userId":userId},@{@"formxml":self.body}];

[HttpTools postWithURL:_url arrays:array method:_method success:^(id JSON) {
        [self.view.window makeToast:@"提交通知成功"];
        [self.navigationController popViewControllerAnimated:YES];
    } failure:^(NSError *error) {
        [self.view.window makeToast:@"提交通知失败"];
    }];




}
#pragma mark 设置右边的view
-(void)setupTextFieldRightView{
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(keybordDown)];
    [self.view addGestureRecognizer:tap];
    
    UIView * rightView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
    rightView.backgroundColor = [UIColor clearColor];
    UIButton * addNameBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [addNameBtn setImage:[UIImage imageNamed:@"addName.png"] forState:UIControlStateNormal];
    addNameBtn.frame = CGRectMake(-5, 0, 20, 20);
    [addNameBtn addTarget:self action:@selector(addNameAction:) forControlEvents:UIControlEventTouchUpInside];
    [rightView addSubview:addNameBtn];
    
    self.subjectTextField.rightView = rightView;
    self.subjectTextField.rightViewMode=UITextFieldViewModeAlways;
}
#pragma mark 发送范围

-(void)addNameAction:(UIButton*)btn{
    
//    OACityNameViewController * nameVC = [[OACityNameViewController alloc]init];
//    __weak typeof(self) weakSelf = self;
//    nameVC.GetNameStringBlock = ^(NSString * str){
//        weakSelf.subjectTextField.text = str;
//    };
//    [self.navigationController pushViewController:nameVC animated:YES];
OANameListController * listVC = (OANameListController*)[self.storyboard instantiateViewControllerWithIdentifier:@"NameLIstID"];
    listVC.delegate = self;
    UINavigationController * nav = [[UINavigationController alloc]initWithRootViewController:listVC];
    [self presentViewController:nav animated:YES completion:nil];

}
-(void)didSelecteName:(NSArray*)names{
    
    NSMutableArray * namesArray = [NSMutableArray array];
    for (OASubNameListModel * model in names) {
        [namesArray addObject:model.value];
    }
   
    self.subjectTextField.text = [namesArray componentsJoinedByString:@","];
    
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    if (textField == self.subjectTextField) {
        return NO;
    }
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}
- (void)keybordDown {
    [self.contentText resignFirstResponder];
    [self.sendTextField resignFirstResponder];
    [self.subjectTextField resignFirstResponder];
}

// 设置时间
- (IBAction)button:(id)sender {
    _selectDatePicker = [[MHDatePicker alloc] init];
    _selectDatePicker.isBeforeTime = YES;
    _selectDatePicker.datePickerMode = UIDatePickerModeDate;
    
    __weak typeof(self) weakSelf = self;
    [_selectDatePicker didFinishSelectedDate:^(NSDate *selectedDate) {
//        NSString *string = [NSString stringWithFormat:@"%@",[NSDate dateWithTimeInterval:3600*8 sinceDate:selectedDate]];
//        weakSelf.myLabel2.text = string;
        weakSelf.datalabel.text = [weakSelf dateStringWithDate:selectedDate DateFormat:@"yyyy年MM月dd日"];
    }];
}
- (NSString *)dateStringWithDate:(NSDate *)date DateFormat:(NSString *)dateFormat
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:dateFormat];
    [dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"]];
    NSString *str = [dateFormatter stringFromDate:date];
    return str ? str : @"";
}
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{
    [UIView animateWithDuration:0.2 animations:^{
    self.view.transform = CGAffineTransformMakeTranslation(0, -100);
    }];
  
    return YES;
}


- (void)textViewDidEndEditing:(UITextView *)textView{
    
    [UIView animateWithDuration:0.2 animations:^{
         self.view.transform = CGAffineTransformIdentity;
    }];
   
    
}
@end
